//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.xianggg.xservers.weixin.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CrossFilter implements Filter {
    private static final Logger LOG = LoggerFactory.getLogger(CrossFilter.class);

    public CrossFilter() {
    }

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (response instanceof HttpServletResponse) {
            HttpServletResponse alteredResponse = (HttpServletResponse) response;
            this.addHeadersFor200Response(alteredResponse, (HttpServletRequest) request);
        }
        chain.doFilter(request, response);
    }

    private void addHeadersFor200Response(HttpServletResponse response, HttpServletRequest request) {
        String origin = request.getHeader("Origin");
//        LOG.debug(LOG.isDebugEnabled() ? "addHeadersFor200Response  head origin:" + origin : null);
        response.addHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Cookie");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.addHeader("Access-Control-Max-Age", "1728000");
        response.addHeader("Access-Control-Allow-Credentials", "true");

    }

    public void destroy() {
    }
}
